package com.haojie.client;

import com.haojie.common.Blog;
import com.haojie.service.BlogService;

public class RPCClient1 {
    public static void main(String[] args) {
//        try {

//            RPCClientProxy RPCClientProxy = new RPCClientProxy();
//            BlogService proxy = RPCClientProxy.getProxy(BlogService.class);

//            SimpleRPCClient simpleRPCClient = new SimpleRPCClient("127.0.0.1", 8899);
//            RPCClientProxy rpcClientProxy = new RPCClientProxy(simpleRPCClient);
//            BlogService blogService = rpcClientProxy.getProxy(BlogService.class);
//        //调用服务方法
//            Blog userById = blogService.getBlogById(10);
//            System.out.println("从服务端得到的user为：" + userById);

            NettyRPCClient nettyRPCClient = new NettyRPCClient("127.0.0.1", 8899);
            RPCClientProxy nettyRPCClientProxy = new RPCClientProxy(nettyRPCClient);
            BlogService blogService1 = nettyRPCClientProxy.getProxy(BlogService.class);
            Blog userById1 = blogService1.getBlogById(1000);
            System.out.println("从服务端得到的blog为：" + userById1);

//            BlogService blogService = proxy.getProxy(BlogService.class);
//            Blog blogById = proxy.getBlogById(10000);
//            System.out.println("从服务端得到的blog为：" + blogById);

//            User user = User.builder().id(100).userName("张三").sex(true).build();
//            Integer integer = proxy.insertUserId(user);
//            System.out.println("向服务端插入数据："+integer);

//            // 建立Socket连接
//            Socket socket = new Socket("127.0.0.1", 8899);
//            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
//            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
//            // 传给服务器id
//            objectOutputStream.writeInt(new Random().nextInt());
//            objectOutputStream.flush();
//            // 服务器查询数据，返回对应的对象
//            User user  = (User) objectInputStream.readObject();
//            System.out.println("服务端返回的User:"+user);

//        } catch (IOException | ClassNotFoundException e) {
//            e.printStackTrace();
//            System.out.println("客户端启动失败");
//        }
    }
}
